<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ParentController
 *
 * @author Schorr
 */
class ParentController extends Zend_Controller_Action
{

    public function init()
    {
	$this->_helper->acl->allow('admin')
		->allow('user', array('index', 'list'));
    }

    public function indexAction()
    {
	$this->listAction();
    }

    //list all parents
    public function listAction()
    {
	$m = new Model_Parent;
	$parent = $m->listAll();
	$this->view->parent = $parent;
    }

    //create a new parent
    public function createAction()
    {
	$request = $this->getRequest();
	/* $request_from => which page calls
	 * 0 = parent list -> parent_list
	 * 1 = create a new application -> child_report/id
	 * 2 = child report/id ->  child_report/id
	 */
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');
	$child_id = (int) $request->getParam('child_id');

	// creates a form of 2 subforms for person details and address
	$form = new Zend_Form;
	$form->addSubForm(new Application_Form_Parent, 'parent');
	$form->addSubForm(new Application_Form_Address, 'address');
	$form->addElement('submit', 'submit', array(
	    'ignore' => true,
	    'label' => 'Elternteil anlegen',
	));

	if ($request->isPost())
	{
	    if ($form->isValid($request->getPost()))
	    {
		// save the address
		$addressData = $form->getSubForm('address')->getValues(true);
		$addressModel = new Model_Address;
		$addressModel->create($addressData);
		$lasCreatedAddress = $addressModel->lastCreated();

		// save the parent
		$parentData = $form->getSubForm('parent')->getValues(true);
		$parentData['idaddress'] = $lasCreatedAddress['id'];
		$parentModel = new Model_Parent;
		$created_parent_id = $parentModel->create($parentData);

		// request come from create Request
		if ($request_from == 0)
		{
		    return $this->_helper->redirector('list');
		}
		// request come from create new application
		if ($request_from == 1)
		{
		    //allocate new created parent with child
		    $data = array('idperson1' => $child_id, 'idperson2' => $created_parent_id);
		    $m = new Model_Allocate;
		    $m->allocate_create($data);

		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
		}
		// request come from report child
		if ($request_from == 2)
		{
		    //allocate new created parent with child
		    $data = array('idperson1' => $child_id, 'idperson2' => $created_parent_id);
		    $m = new Model_Allocate;
		    $m->allocate_create($data);

		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
		}
		if ($request_from == 3)
		{
		    //allocate new created parent with child
		    $data = array('idperson1' => $child_id, 'idperson2' => $created_parent_id);
		    $m = new Model_Allocate;
		    $m->allocate_create($data);

		    return $this->_helper->redirector->gotoURL('/child/list');
		}
	    }
	}
	$this->view->form = $form;
    }

    public function updateAction()
    {
	$request = $this->getRequest();
	/* $request_from => which page calls
	 * 0 = parent list -> parent_list
	 * 1 = create a new application -> child_report/id
	 * 2 = child report/id ->  child_report/id
	 */
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');
	$child_id = (int) $request->getParam('child_id');

	$modelParent = new Model_Parent;
	$parentWithAddress = $modelParent->findByIdWithAddress($id);

	//var_dump($parentWithAddress);

	if ($parentWithAddress == NULL)
	{
	    throw new Zend_Controller_Action_Exception('This page does not exist', 404);
	}

	$form = new Zend_Form;
	$formParent = new Application_Form_Parent;
	$formParent->excludePersonIdFromDbExistEmailValidation($id);
	$form->addSubForm($formParent, 'parent');
	$form->addSubForm(new Application_Form_Address, 'address');
	$form->addElement('submit', 'update', array(
	    'ignore' => true,
	    'label' => 'Änderung speichern',
	));

	$modelAddress = new Model_Address;

	if ($request->isPost())
	{
	    if ($form->isValid($request->getPost()))
	    {
		$addressValues = $form->getSubForm('address')->getValues(true);
		$addressValues['id'] = $parentWithAddress['idaddress'];
		$modelAddress->update($addressValues);

		$parentValues = $form->getSubForm('parent')->getValues(true);
		$parentValues['id'] = $parentWithAddress['id'];
		$modelParent->update($parentValues);

		// request come from create Request
		if ($request_from == 0)
		{
		    return $this->_helper->redirector('list');
		}
		// request come from create new application
		if ($request_from == 1)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
		}
		// request come from report child
		if ($request_from == 2)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
		}
	    }
	} else
	{
	    $form->populate($parentWithAddress);
	}

	$this->view->form = $form;
    }

    public function deleteAction()
    {
	$request = $this->getRequest();
	/* $request_from => which page calls
	 * 0 = parent list -> parent_list
	 * 1 = create a new application -> child_report/id
	 * 2 = child report/id ->  child_report/id
	 */
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');
	$child_id = (int) $request->getParam('child_id');
	;
	$plp_id = (int) $request->getParam('plp_id');
	;

	$m = new Model_Parent;
	// geht bestimmt besser die idaddress zu bekommen oder?
	$parentWithAddress = $m->findByIdWithAddress($id);
	$idaddress = $parentWithAddress['idaddress'];

	//delete all Entry in PersonLinkPerson with this id
	$a = new Model_Allocate;
	$a->allocate_deleteByPersonIdAction($id);
	//delete child in Person
	$m->delete($id);
	// delete 
	$a = new Model_Address;
	$a->delete($idaddress);



	// request come from create Request
	if ($request_from == 0)
	{
	    return $this->_helper->redirector('list');
	}
	// request come from create new application
	if ($request_from == 1)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
	}
	// request come from report child
	if ($request_from == 2)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $child_id);
	}
    }

    public function infoAction()
    {
	/* $request_from => which page calls
	 * 0 = parent list -> parent_list
	 * 1 = create a new application -> child_report/id
	 * 2 = child report/id ->  child_report/id
	 */
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');
	$child_id = (int) $request->getParam('child_id');


	$m = new Model_Parent;
	$parent = $m->findByIdWithAddress($id);
	$birthday = new Zend_Date($parent['birthday'], 'yyyy-MM-dd');
	$parent['birthday'] = $birthday->get("dd.MM.yyyy");

	$this->view->parent = $parent;
	$this->view->request_from = $request_from;
	$this->view->child_id = $child_id;
    }

}

?>
